var server_url = 'php/server.php';
//Check if action attribute (which indicates server) of form tag is set, then choose it (low 

//获取简介
function getIntroduction(){
	var form_data = {};
	form_data.submit_message = "getIntroduction";
	$.post(server_url,form_data,function(json) {
		json = JSON.parse(json);
	    if (json.result=="1"||json.result=='1'||json.result==1) {
		   document.getElementById('introduction').value = json.info;
	    }
	    else {
	        alert('获取失败'+json.reason);
	    }
	})
}

//提交简介
function modifyIntroduction(){
	var form_data = {};
	form_data.submit_message = "modifyIntroduction";
	form_data.introduction = document.getElementById('introduction').value;
	$.post(server_url,form_data,function(json) {
		json = JSON.parse(json);
	    if (json.result=="1"||json.result=='1'||json.result==1) {
		   document.getElementById('introduction').value = json.info;
		   alert('修改成功！');
		   document.getElementById('introduction').readOnly = 'readonly';
	    }
	    else {
	        alert('修改失败！'+json.reason);
	    }
	})
}

//获取网站列表
function getPageList(){
	
	document.getElementById('pageListShowDiv').style.display='block';
	document.getElementById('pageListAddDiv').style.display='none';
	document.getElementById('pageListModDiv').style.display='none';
	
	var form_data = {};
	form_data.submit_message = "getPageList";
	$.post(server_url,form_data,function(json) {
		json = JSON.parse(json);
	    if (json.result=="1"||json.result=='1'||json.result==1) {
		   let html="";
		   let list = json.info;
		   for(let i=0;i<list.length;i++){
			   let item=list[i];
			   html=html+"<tr><td>"+(i+1)+"</td>"+
						"<td>"+item[1]+"<br>"+item[2]+"</td>"+
						"<td><button class=\"btn3\" onclick='getModifyPage("+item[0]+")'>修改</button>"+
						"<button class=\"btn3\" onclick='deletePage("+item[0]+")'>删除</button></td><tr>";	
		   }
		   $("#tbody").html(html);
	    }
	    else {
	        alert('获取失败pageList'+json.reason);
	    }
	})
}

//修改page信息
function modifyPage(){	
	var form_data = {};
	form_data.submit_message = "modifyPage";
	form_data.id = document.getElementById('pageId').value;
	form_data.pageName = document.getElementById('pageName').value;
	form_data.url = document.getElementById('url').value;
	$.post(server_url,form_data,function(json) {
		json = JSON.parse(json);
	    if (json.result=="1"||json.result=='1'||json.result==1) {
		   alert('修改成功！');
		   getPageList();
	    }
	    else {
	        alert('修改失败！'+json.reason);
	    }
	})
}

//获取page信息by id
function getModifyPage(id){
	document.getElementById('pageListShowDiv').style.display='none';
	document.getElementById('pageListAddDiv').style.display='none';
	document.getElementById('pageListModDiv').style.display='block';
	
	var form_data = {};
	form_data.submit_message = "getPageById";
	form_data.id = id;
	$.post(server_url,form_data,function(json) {
		json = JSON.parse(json);
	    if (json.result=="1"||json.result=='1'||json.result==1) {
		   document.getElementById('pageId').value=id;
			document.getElementById('pageName').value=json.info[0][0];
			document.getElementById('url').value=json.info[0][1];
	    }
	    else {
	        alert('获取信息失败！'+json.reason);
	    }
	})
}
//删除page信息
function deletePage(id){
	let r = confirm('确认删除？');
	if(r){
		var form_data = {};
		form_data.submit_message = "deletePage";
		form_data.id = id;
		$.post(server_url,form_data,function(json) {
			json = JSON.parse(json);
		    if (json.result=="1"||json.result=='1'||json.result==1) {
			   alert('删除成功！');
			   getPageList();
		    }
		    else {
		        alert('删除失败！'+json.reason);
		    }
		})
	}
}
//新增界面
function showAddDiv(){
	document.getElementById('pageListShowDiv').style.display='none';
	document.getElementById('pageListAddDiv').style.display='block';
	document.getElementById('pageListModDiv').style.display='none';
}
//新增page
function addPage(){
	var form_data = {};
	form_data.submit_message = "addPage";
	form_data.pageName = document.getElementById('newPageName').value;
	form_data.url = document.getElementById('newUrl').value;
	$.post(server_url,form_data,function(json) {
		json = JSON.parse(json);
	    if (json.result=="1"||json.result=='1'||json.result==1) {
		   alert('添加成功！');
		   getPageList();
	    }
	    else {
	        alert('添加失败！'+json.reason);
	    }
	})
}
//返回到pageList界面
function returnToPageList(){
	document.getElementById('pageListShowDiv').style.display='block';
	document.getElementById('pageListAddDiv').style.display='none';
	document.getElementById('pageListModDiv').style.display='none';
}

//获取留言
function getMessageList(){
	var form_data = {};
	form_data.submit_message = "getMessageList";
	$.post(server_url,form_data,function(json) {
		json = JSON.parse(json);
	    if (json.result=="1"||json.result=='1'||json.result==1) {
		   let html="";
		   let list = json.info;
		   for(let i=0;i<list.length;i++){
			   let item=list[i];
			   html=html+"<tr><td>"+(i+1)+"</td>"+
						"<td>"+item[1]+"<br>"+item[2]+"<br>"+item[4]+"<br>"+item[3]+"</td>"+
						// "<td>"+item[2]+"</td>"+
						// "<td>"+item[4]+"</td>"+
						// "<td>"+item[3]+"</td>"+
						"<td>"+
						"<button class=\"btn3\" onclick='deleteMsg("+item[0]+")'>删除</button>"+
						"</td></tr>";	
		   }
		   $("#msgTbody").html(html);
	    }
	    else {
	        alert('获取失败msgList'+json.reason);
	    }
	})
}

//删除某条留言
function deleteMsg(id){
	let r = confirm('确认删除？');
	if(r){
		var form_data = {};
		form_data.submit_message = "deleteMsg";
		form_data.id = id;
		$.post(server_url,form_data,function(json) {
			json = JSON.parse(json);
		    if (json.result=="1"||json.result=='1'||json.result==1) {
			   alert('删除成功！');
			   getMessageList();
		    }
		    else {
		        alert('删除失败！'+json.reason);
		    }
		})
	}
}

getIntroduction();
getPageList();
getMessageList();